/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "LOGR_browsers.h"

WebBrowserNodePtr
new_web_browser_node(void)
{
  WebBrowserNodePtr wbn = (WebBrowserNodePtr)calloc(1, sizeof(WebBrowserNodeStruct));
  if(!wbn) {
      fprintf(stderr,"Out of Memory new_web_browser_node\n");
      abort();
  }
  return wbn;
}

void
delete_web_browser_node(WebBrowserNodePtr wbn)
{	
    if(!wbn) return;

    if(wbn->ua) {
	free(wbn->ua); wbn->ua = NULL;
    }
    free(wbn); wbn = NULL;
}

void
LOGR_set_user_agent(WebBrowserNodePtr wbn,char *sua)
{
  if(!wbn) return;
  if(!sua) return;
  GRStrAllocCopy(wbn->ua,sua);
}

const char *
LOGR_get_user_agent(WebBrowserNodePtr wbn)
{
  if(!wbn) return NULL;
  return wbn->ua;
}

void
LOGR_set_is_spider(WebBrowserNodePtr wbn)
{
  if(!wbn) return;
  wbn->is_spider = 1;
}

void
LOGR_set_is_browser(WebBrowserNodePtr wbn)
{
  if(!wbn) return;
  wbn->is_spider = 0;
}

short
LOGR_is_spider(WebBrowserNodePtr wbn)
{
  if(!wbn) return NULL;
  return wbn->is_spider;
}

void
LOGR_set_browser_naccesses(WebBrowserNodePtr wbn,long num)
{
  if(!wbn) return;
  wbn->naccesses = num;
}

void
LOGR_inc_browser_naccesses(WebBrowserNodePtr wbn)
{
  if(!wbn) return;
  wbn->naccesses++;
}

long
LOGR_get_browser_naccesses(WebBrowserNodePtr wbn)
{
  if(!wbn) return 0;
  return wbn->naccesses;
}

void
LOGR_set_browser_npages(WebBrowserNodePtr wbn,long num)
{
  if(!wbn) return;
  wbn->npages = num;
}

void
LOGR_inc_browser_npages(WebBrowserNodePtr wbn)
{
  if(!wbn) return;
  wbn->npages++;
}

long
LOGR_get_browser_npages(WebBrowserNodePtr wbn)
{
  if(!wbn) return 0;
  return wbn->npages;
}


void
LOGR_set_browser_sizebytes(WebBrowserNodePtr wbn,double num)
{
  if(!wbn) return;
  wbn->sbytes = num;
}

void
LOGR_add_browser_sizebytes(WebBrowserNodePtr wbn,double num)
{
  if(!wbn) return;
  wbn->sbytes += num;
}

double
LOGR_get_browser_sizebytes(WebBrowserNodePtr wbn)
{
  if(!wbn) return 0;
  return wbn->sbytes;
}


int
LOGR_is_accessb_lower(WebBrowserNodePtr u, WebBrowserNodePtr v)
{
  return (u->naccesses < v->naccesses);
}

int
LOGR_is_accessb_bigger(WebBrowserNodePtr u,WebBrowserNodePtr v)
{
  return(u->naccesses > v->naccesses);
}

TypeVersion
LOGR_extract_ua_version(WebBrowserNodePtr wbn)
{
  const char *nsv5 = "/5.";
  const char *nsv4 = "/4.";
  const char *nsv3 = "/3.";
  const char *nsv2 = "/2.";
  const char *msv5 = "MSIE 5.";
  const char *msv4 = "MSIE 4.";
  const char *msv3 = "MSIE 3.";
  const char *msv2 = "MSIE 2.";
  const char *operav3 = "Opera 3.";
  const char *lynxv2 = "Lynx 2.";

    if(wbn && wbn->ua)
    {
	if(wbn->tb < 0)
	    LOGR_extract_ua_browser(wbn);
	if(wbn->tb > 0)
	{
	    if(wbn->tb == BNETSCAPE)
	    {
		if(strstr(wbn->ua,nsv5))
		    wbn->tv = V5;
		else if(strstr(wbn->ua,nsv4))
		    wbn->tv = V4;
		else if(strstr(wbn->ua,nsv3))
		    wbn->tv = V3;
		else if(strstr(wbn->ua,nsv2))
		    wbn->tv = V2;
		else
		    wbn->tv = OTHER_VERSION;
	    }
	    else if(wbn->tb == BMSIE)
	    {
		if(strstr(wbn->ua,msv5))
		    wbn->tv = V5;
		else if(strstr(wbn->ua,msv4))
		    wbn->tv = V4;
		else if(strstr(wbn->ua,msv3))
		    wbn->tv = V3;
		else if(strstr(wbn->ua,msv2))
		    wbn->tv = V2;
		else
		    wbn->tv = OTHER_VERSION;

	    }
	    else if(wbn->tb == BOPERA)
	    {
		if(strstr(wbn->ua,operav3))
		    wbn->tv = V3;
		else
		    wbn->tv = OTHER_VERSION;
	    }
	    else if(wbn->tb == BLYNX)
	    {
		if(strstr(wbn->ua,lynxv2))
		    wbn->tv = V2;
		else
		    wbn->tv = OTHER_VERSION;
	    }
	    else
		wbn->tv = OTHER_VERSION;
	}
	return wbn->tv;
    }
    return 0;
}



short
maybe_is_user_agent_spider(char *ua_name)
{
  const char *smicrosoft_data="Microsoft Data";
  const char *smicrosoft_url="Microsoft URL";
  const char *sfrontpage="MSFrontPage";
  const char *sjava = "Java";
  const char *sdownload = "Download";
  short isspider = 1;

  if(strstr(ua_name,smicrosoft_data))
      isspider = 0;
  else if(strstr(ua_name,sfrontpage))
      isspider = 0;
  else if(strstr(ua_name,smicrosoft_url))
      isspider = 0;
  else if(strstr(ua_name,sjava))
      isspider = 0;
  else if(strstr(ua_name,sdownload))
      isspider = 0;

  return isspider;
}

TypeBrowser
LOGR_extract_ua_browser(WebBrowserNodePtr wbn)
{
  const char *scomp = "compatible";
  const char *smsie = "MSIE";
  const char *smozilla = "Mozilla";
  const char *sopera = "Opera";
  const char *slynx = "Lynx";

  TypeBrowser tb = 0;

  wbn->is_spider = 0;
  
  if(wbn && wbn->ua)
  {
      if(strstr(wbn->ua,sopera))
      {
	  tb = BOPERA;	
      }
      else if(strstr(wbn->ua,slynx))
      {
	  tb = BLYNX;
      }
      else if(strstr(wbn->ua,smozilla))
      {
	  if(strstr(wbn->ua,scomp))
	  {
	      if(strstr(wbn->ua,smsie))
	      {
		  tb = BMSIE;
	      }
	      else
	      {
		  tb = OTHER_NETSCAPE_COMP;
	      }
	  }
	  else
	  {
	      tb = BNETSCAPE;
	  }
      }
      else
      {
	  tb = OTHER_BROWSER;
	  if(maybe_is_user_agent_spider(wbn->ua))
	      wbn->is_spider = 1;
      }
      wbn->tb = tb;
  }
  return tb;
}


TypePlatform
LOGR_extract_ua_platform(WebBrowserNodePtr wbn)
{
  const char *swindows95 = "Windows 95";
  const char *swindows98 = "Windows 98";
  const char *swin95 = "Win95";
  const char *swin98 = "Win98";
  const char *swindowsNT = "Windows NT";
  const char *swinNT = "WinNT";
  const char *sX11= "X11";
  const char *sMac= "Macintosh";
  const char *swindows = "Win";
  const char *sos2 = "OS/2";
  const char *swindows3 = "Windows 3";
  TypePlatform tp = 0;
  
  if(wbn && wbn->ua)
  {
      if(strstr(wbn->ua,sos2))
	  tp = POS2;
      else if(strstr(wbn->ua,sX11))
	  tp = PUNIX;
      else if(strstr(wbn->ua,sMac))
	  tp = PMACINTOSH;
      else if(strstr(wbn->ua,swindows95))
	  tp = PWINDOWS95;
      else if(strstr(wbn->ua,swin95))
    	  tp = PWINDOWS95;
      else if(strstr(wbn->ua,swindows98))
	  tp = PWINDOWS98;
      else if(strstr(wbn->ua,swin98))
    	  tp = PWINDOWS98;
      else if(strstr(wbn->ua,swindowsNT))
	  tp = PWINDOWSNT;
      else if(strstr(wbn->ua,swinNT))
	  tp = PWINDOWSNT;
      else if(strstr(wbn->ua,swindows3))
    	  tp = PWINDOWS31;
      else if(strstr(wbn->ua,swindows))
    	  tp = PWINDOWS31;
      else
	  tp = OTHER_PLATFORM;

      wbn->tp = tp;
  }
  return tp;
}	

TypeVersion
LOGR_get_ua_version(WebBrowserNodePtr wbn)
{
    if(wbn)
	return wbn->tv;
    else
	return -1;
}


TypeBrowser
LOGR_get_ua_browser(WebBrowserNodePtr wbn)
{
    if(wbn)
	return wbn->tb;
    else
	return -1;
}


TypePlatform
LOGR_get_ua_platform(WebBrowserNodePtr wbn)
{
    if(wbn)
	return wbn->tp;
    else
	return -1;
}

void
LOGR_set_ua_platform(WebBrowserNodePtr wbn,TypePlatform tp)
{
    if(wbn)
	wbn->tp = tp;
}

void
LOGR_set_ua_version(WebBrowserNodePtr wbn,TypeVersion tv)
{
    if(wbn)
	wbn->tv = tv;
}

void
LOGR_set_ua_browser(WebBrowserNodePtr wbn,TypeBrowser tb)
{
    if(wbn)
	wbn->tb = tb;
}

const char *
LOGR_string_browser(WebBrowserNodePtr wbn)
{
    if(!wbn)
	return NULL;

    switch(wbn->tb)
    {	
    case BNETSCAPE:
	return "Netscape";
    case BMSIE:
	return "Internet Explorer";
    case OTHER_NETSCAPE_COMP:
	return "Netscape Compatible";
    case BOPERA:
	return "Opera";
    case BLYNX:
	return "Lynx";
    case OTHER_BROWSER:
	return "Others";
    }
    return NULL;
}

const char *
LOGR_string_version(WebBrowserNodePtr wbn)
{
    if(!wbn)
	return NULL;
	
    switch(wbn->tv)
    {
    case V2:
	return "V2.*";
    case V3:
	return "V3.*";
    case V4:
	return "V4.*";
    case V5:
	return "V5.*";
    case OTHER_VERSION:
	return "V*.*";
    }
    return NULL;
}

const char *
LOGR_string_platform(WebBrowserNodePtr wbn)
{
    if(!wbn)
	return NULL;
	
    switch(wbn->tp)
    {
    case PWINDOWS98 :
	return "Windows 98";
    case PWINDOWS95:
	return "Windows 95";
    case PWINDOWSNT:
	return "Windows NT";
    case PMACINTOSH:
	return "Macintosh";
    case PUNIX:
	return "Unix";
    case PWINDOWS31:
	return "Windows 3.* and lower";
    case POS2:
	return "OS/2";
    case OTHER_PLATFORM:
	return "other platform";
    }
    return NULL;
}




